package server.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import server.bean.Page;
import server.bean.Product;
import server.bean.Shop;
import server.mapper.ProductMapper;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Log4j
@RestController
public class ProductController {
    @Autowired
    ProductMapper productMapper;

    @RequestMapping("/product")
    public PageInfo<Product> getProduct(@RequestBody Page page){
        PageHelper.startPage(page.getPage(), page.getPageSize());
//        return productMapper.selectPartProduct(page.getPageStart(), page.getPageNum());
        List<Product> list = productMapper.selectPartProduct();
        PageInfo<Product> pageInfo = new PageInfo<>(list,5);
        return pageInfo;
//        return new PageResult(page.getTotal(), page.getResult());
    }

    @RequestMapping("/allproduct")
    public List<Product> getAllProduct(){
        return productMapper.selectProduct();
    }


    @PostMapping("/selectbyname")
    public List<Product> selectByName (@RequestBody Product product) {
        return productMapper.selectByName(product.getProductName());
    }
    @PostMapping("/deletebyid")
    public void deleteById (@RequestBody Product product){
        productMapper.deleteById(product.getProductId());
//        return ;
    }

//    @CrossOrigin(origins = {"http://localhost:3000", "null"})
    @PostMapping("/updatebyid")
    public void updateById(@RequestBody Product product){
        productMapper.updataById(product);
    }
    @PostMapping("insert")
    public void insert(@RequestBody Product product)  {
        productMapper.insert(product);
    }

    @GetMapping("/selectproductnumber")
    public Integer selectProductNumber(){
        return productMapper.selectProductNumber();
    }


}
